package com.caiyouxi.goods.service;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.goods.mapper.GoodsPropertiesGroupMapper;
import com.caiyouxi.goods.model.GoodsPropertiesGroup;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/11/9.
 */
@Service
public class GoodsPropertiesGroupService extends BaseService{

    private Logger logger = Logger.getLogger(GoodsPropertiesGroupService.class);

    @Autowired
    private GoodsPropertiesGroupMapper goodsPropertiesGroupMapper;

    /**
     * 查询商品属性分组
     */
    public ResData get_goods_properties_group_info(GoodsPropertiesGroup goodsPropertiesGroup){
        String redis_key = "GOODS_PROPERTIES_GROUP"+goodsPropertiesGroup.getId();
        logger.info("search redis...");
        GoodsPropertiesGroup newGoodsPropertiesGroup = (GoodsPropertiesGroup) redisService.get(redis_key);
        if (null == newGoodsPropertiesGroup){
            logger.info("search db...");
            newGoodsPropertiesGroup  =goodsPropertiesGroupMapper.select_goods_properties_group(goodsPropertiesGroup.getId());
            redisService.set(redis_key,newGoodsPropertiesGroup);
        }
        return  success(newGoodsPropertiesGroup);
    }

    /**
     * 查询商品属性分组列表
     */
    public ResData get_goods_properties_group_list(GoodsPropertiesGroup goodsPropertiesGroup){
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            goodsPropertiesGroup.setPage_size(reids_page_size);
        }

        List<GoodsPropertiesGroup> goodsDiscounts = goodsPropertiesGroupMapper.select_goods_properties_group_list(goodsPropertiesGroup);
        int total = goodsPropertiesGroupMapper.select_goods_properties_group_count(goodsPropertiesGroup);
        QueryParams pageList = new QueryParams();
        pageList.setTotal(total);
        pageList.setPage(goodsPropertiesGroup.getPage());
        pageList.setPage_size(goodsPropertiesGroup.getPage_size());
        pageList.setList(goodsDiscounts);
        return success(pageList);
    }

    /**
     * 新增商品属性分组
     */
    @Transactional
    public ResData add_goods_properties_group(GoodsPropertiesGroup goodsPropertiesGroup){
        int result =goodsPropertiesGroupMapper.add_goods_properties_group(goodsPropertiesGroup);
        if(result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCROUP_ADD_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCROUP_ADD_ERROR.getMsg(),null);
    }

    /**
     * 修改商品属性分组
     */
    @Transactional
    public ResData update_goods_properties_group(GoodsPropertiesGroup goodsPropertiesGroup){
        int result = goodsPropertiesGroupMapper.update_goods_properties_group(goodsPropertiesGroup);
        if(result == 1){
            String redis_key = "GOODS_PROPERTIES_GROUP"+goodsPropertiesGroup.getId();
            redisService.set(redis_key,goodsPropertiesGroup);  //更新缓存内容
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCROUP_UPDATE_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCROUP_UPDATE_ERROR.getMsg(),null);
    }

    /**
     * 删除商品属性分组
     */
    @Transactional
    public ResData delete_goods_properties_group(GoodsPropertiesGroup goodsPropertiesGroup){
        int result =goodsPropertiesGroupMapper.delete_goods_properties_group(goodsPropertiesGroup.getId());
        if(result == 1){
            String redis_key = "GOODS_PROPERTIES_GROUP"+goodsPropertiesGroup.getId();
            redisService.remove(redis_key);  //删除缓存
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCROUP_DELTET_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCROUP_DELTET_ERROR.getMsg(),null);
    }




}
